import { WeaTableNew, WeaForm } from 'comsMobx'
import { each } from 'lodash';
import { observable, action, computed } from 'mobx';
import API from '../../../apis/task';
import * as Action from './action';
const { TableStore } = WeaTableNew;
export default class TaskListStore {
    // left
    @observable leftDatas = [];
    @observable selectedApp = '';
    @observable searchValue = '';

    // right
    @observable selectedTab = 'doing';
    @observable tableStore = new TableStore();
    @observable topCount = {};

    @observable conditionVisible = false;
    @observable conditionForm = new WeaForm();
    conditions;
    constructor() {
        each(Action, (value, key) => {
            this[key] = action(value.bind(this));
        });
        this.conditions = this.getConditions();
        this.conditionForm.initFormFields(this.conditions);
    }
    @computed get apps() {
        return this.leftDatas.filter(d => d.name.toLowerCase().indexOf(this.searchValue.toLowerCase()) > -1);
    }
    @computed get quickSearchValue(){
        if (this.conditionForm.isFormInit && this.conditionForm.has('taskName')) {
            return this.conditionForm.$('taskName').values();
        } 
        return '';
    }
    @action
    changeTab = (selectedTab) => {
        this.selectedTab = selectedTab;
        this.tableStore = new TableStore();
        this.init();
        this.initTable();
    }
    @action changeSelectedApp = (selectedApp) => {
        this.changeQuickSearchValue('');
        this.selectedApp = selectedApp;
        this.initTable();
        this.getTopCount();
    }
    @action setLeftSearchValue = (value) => {
        this.searchValue = value;
    }
    @action
    init = () => {
        API.left(this.selectedTab).then(this.setLeftData);
        this.getTopCount();
        this.initTable();
    }
    @action getTopCount = () => {
        API.topCount(this.selectedApp).then(this.setTopCount);
    }
    @action
    setLeftData = (data) => {
        this.leftDatas = data;
    }
    @action
    setTopCount = (data) => {
        this.topCount = data;
    }
    @action
    initTable = () => {
        API.splitKey({
            type: this.selectedTab,
            appid: this.selectedApp,
            quickSearchValue: this.quickSearchValue,
            ...this.conditionForm.getFormParams()
        }).then(this.fetchDatas);
    }
    @action
    fetchDatas = (data) => {
        this.tableStore.getDatas(data.datas);
    }
    @action reloadTable = () => {
        this.tableStore.reloadCurrent();
        this.init();
    }
}